﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterController : MonoBehaviour
{
    public static CharacterController _instance;
    private Rigidbody2D rb;
    [Header("移动参数")]
    public float speed;
    private float moveInput;

    [Header("跳跃参数")]
    public float jumpForce;//跳跃的初始蓄力
    public float addJumpForce;//跳跃的增加蓄力
    public float jumpForceMax;//跳跃的最大蓄力
    public float jumpTime;//跳跃的最大蓄力时间
    public float startTime;//跳跃的初始蓄力时间
    public float timeJump;//跳跃当前的蓄力时间

    public bool isJumping;
    public bool xuLi;
    public bool xiaLuo;

    public float g;//下落的力
    public float Z;//下坠的力

    [Header("地板射线检测参数")]
    private bool isGround;
    public Transform feetPos;
    public float checkRadius;
    public LayerMask whatIsGround;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        _instance = this;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        FilpDirction();
        moveInput = Input.GetAxisRaw("Horizontal");
        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    }
    void Update()
    {
        //地板检测
        isGround = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);

        if (Input.GetKey(KeyCode.Space))
        {

        }

        //跳跃
        if (Input.GetKeyDown(KeyCode.Space)&&isGround)
        {
            isJumping= true;  //进入跳跃状态
            timeJump = 0;//蓄力时间清零
            jumpForceMax+= jumpForce;//赋予跳跃的初始蓄力
        }
        else if (Input.GetKey(KeyCode.Space)  && isJumping)
        {
            timeJump += Time.deltaTime;//蓄力时间增加
            if (timeJump < startTime)
            {
                xiaLuo = true;
                rb.velocity = new Vector2(rb.velocity.x, jumpForceMax);
            }
            else if (timeJump < jumpTime)
            {
                xuLi = true;
               jumpForceMax += addJumpForce;//蓄力
                rb.velocity = new Vector2(rb.velocity.x, jumpForceMax);
            }
            else
            {
                jumpForceMax = 0;//蓄力清零
                isJumping = false;
            }
        }
        else if (Input.GetKeyUp(KeyCode.Space))
        {
            jumpForceMax = 0;//蓄力清零
            isJumping = false;//退出跳跃状态
            timeJump = 0;//蓄力时间清零
        }

        //下落速度变快
        if (!isGround && xiaLuo&& rb.velocity.y<=0)
        {
            rb.AddForce(new Vector2(0, -g));
        }

            //Shift快速下落
            if (!isGround&&xuLi)
        {
            if (Input.GetKeyDown(KeyCode.LeftShift))
            {
                rb.velocity = new Vector2(0, rb.velocity.y);
                rb.AddForce(new Vector2(0, -Z));
                xuLi = false;
            }
        }
    }

    void FilpDirction()
    {
        if (moveInput < 0)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }

        if (moveInput > 0)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }
    }
    #region 
    //    if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.K))
    //    {
    //        isJumping = true;
    //        //rb.velocity = Vector2.up * jumpForce;
    //        if (isGround == true )
    //        {
    //            jump= true;
    //            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    //        }
    //    }

    //    if (Input.GetKeyUp(KeyCode.Space) || Input.GetKeyUp(KeyCode.K))
    //    {
    //        jump = false;
    //    }

    //        if (jump == true)
    //    {
    //        jumpTimeCounter -= Time.deltaTime;
    //        if (jumpTimeCounter <= 0 && isJumping == true)
    //        {
    //            rb.velocity = new Vector2(rb.velocity.x, jumpForceMax);
    //            isJumping = false;
    //            jump = false;
    //            jumpTimeCounter = jumpTime;
    //        }

    //    }


    //    else if (isJumping == true && (Input.GetKeyUp(KeyCode.Space) || Input.GetKeyUp(KeyCode.K)))
    //    {
    //        isJumping = false;
    //        jump = false;
    //        jumpTimeCounter = jumpTime;
    //    }
    #endregion
}

